
package View;
//import Controller.Student;
import Model.DBServices;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.KeyEvent;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.UnsupportedLookAndFeelException;

public class changeMyPassword extends javax.swing.JDialog {

    private String oldStudentEmail;
    private int studentId;

    /** Creates new form RegisterNewUser */
    public changeMyPassword(java.awt.Frame parent, boolean modal, Home home) throws UnsupportedLookAndFeelException {
        super(parent, modal);
        initComponents();
        // get the help data form the main frame
        oldStudentEmail =home.getStudentEmail();
        studentId = home.getStudentId();
        
        // fill the id and the user name in the frame
        emailTextField.setText(oldStudentEmail);

        // set the frame in the center of the screan.
        setLocationRelativeTo(null);
        setMinimumSize(new Dimension(this.getWidth(), this.getHeight()));
        //setIconImage(new ImageIcon(getClass().getResource("/Photos/logo.png")).getImage());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        titledPanel = new javax.swing.JPanel();
        titeldPanel = new javax.swing.JPanel();
        userNameLabel = new javax.swing.JLabel();
        passWordOneLabel = new javax.swing.JLabel();
        passWordTwoLabel = new javax.swing.JLabel();
        reNewPasswordField = new javax.swing.JPasswordField();
        newPasswordField = new javax.swing.JPasswordField();
        emailTextField = new javax.swing.JTextField();
        oldPassWordLabel = new javax.swing.JLabel();
        oldPasswordField = new javax.swing.JPasswordField();
        savePasswordButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Change Password");
        setResizable(false);
        addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                formKeyPressed(evt);
            }
        });

        titeldPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Change Password", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Andalus", 1, 14), new java.awt.Color(0, 51, 255))); // NOI18N

        userNameLabel.setFont(new java.awt.Font("Tahoma", 1, 11));
        userNameLabel.setForeground(new java.awt.Color(0, 51, 255));
        userNameLabel.setText("Username");

        passWordOneLabel.setFont(new java.awt.Font("Tahoma", 1, 11));
        passWordOneLabel.setForeground(new java.awt.Color(0, 51, 255));
        passWordOneLabel.setText("New Password");

        passWordTwoLabel.setFont(new java.awt.Font("Tahoma", 1, 11));
        passWordTwoLabel.setForeground(new java.awt.Color(0, 51, 255));
        passWordTwoLabel.setText("Re-Password");

        reNewPasswordField.setFont(new java.awt.Font("Tahoma", 1, 11));
        reNewPasswordField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                reNewPasswordFieldActionPerformed(evt);
            }
        });
        reNewPasswordField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                reNewPasswordFieldKeyPressed(evt);
            }
        });

        newPasswordField.setFont(new java.awt.Font("Tahoma", 1, 11));
        newPasswordField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                newPasswordFieldKeyPressed(evt);
            }
        });

        emailTextField.setEditable(false);
        emailTextField.setFont(new java.awt.Font("Tahoma", 1, 11));
        emailTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                emailTextFieldKeyPressed(evt);
            }
        });

        oldPassWordLabel.setFont(new java.awt.Font("Tahoma", 1, 11));
        oldPassWordLabel.setForeground(new java.awt.Color(0, 51, 255));
        oldPassWordLabel.setText("Current Password");

        oldPasswordField.setFont(new java.awt.Font("Tahoma", 1, 11));
        oldPasswordField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                oldPasswordFieldActionPerformed(evt);
            }
        });
        oldPasswordField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                oldPasswordFieldKeyPressed(evt);
            }
        });

        javax.swing.GroupLayout titeldPanelLayout = new javax.swing.GroupLayout(titeldPanel);
        titeldPanel.setLayout(titeldPanelLayout);
        titeldPanelLayout.setHorizontalGroup(
            titeldPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(titeldPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(titeldPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(passWordTwoLabel, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(passWordOneLabel, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(oldPassWordLabel, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(userNameLabel, javax.swing.GroupLayout.Alignment.TRAILING))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(titeldPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(oldPasswordField)
                    .addComponent(newPasswordField)
                    .addComponent(reNewPasswordField, javax.swing.GroupLayout.DEFAULT_SIZE, 221, Short.MAX_VALUE)
                    .addComponent(emailTextField))
                .addContainerGap(29, Short.MAX_VALUE))
        );
        titeldPanelLayout.setVerticalGroup(
            titeldPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(titeldPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(titeldPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(userNameLabel)
                    .addComponent(emailTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(titeldPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(oldPassWordLabel)
                    .addComponent(oldPasswordField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(titeldPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(newPasswordField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(passWordOneLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(titeldPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(reNewPasswordField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(passWordTwoLabel))
                .addGap(69, 69, 69))
        );

        savePasswordButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/View/accept.png"))); // NOI18N
        savePasswordButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                savePasswordButtonActionPerformed(evt);
            }
        });

        cancelButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/View/cancel.png"))); // NOI18N
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout titledPanelLayout = new javax.swing.GroupLayout(titledPanel);
        titledPanel.setLayout(titledPanelLayout);
        titledPanelLayout.setHorizontalGroup(
            titledPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(titledPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(titledPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, titledPanelLayout.createSequentialGroup()
                        .addComponent(titeldPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, titledPanelLayout.createSequentialGroup()
                        .addComponent(savePasswordButton, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cancelButton, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(117, 117, 117))))
        );
        titledPanelLayout.setVerticalGroup(
            titledPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(titledPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(titeldPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 172, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(titledPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(cancelButton, javax.swing.GroupLayout.DEFAULT_SIZE, 33, Short.MAX_VALUE)
                    .addComponent(savePasswordButton, javax.swing.GroupLayout.DEFAULT_SIZE, 33, Short.MAX_VALUE))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(titledPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(titledPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        this.dispose();
    }//GEN-LAST:event_cancelButtonActionPerformed

    private void savePasswordButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_savePasswordButtonActionPerformed
        validateInputs();
    }//GEN-LAST:event_savePasswordButtonActionPerformed

    private void emailTextFieldKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_emailTextFieldKeyPressed
        if (KeyEvent.VK_ENTER == evt.getKeyCode()) {
            savePasswordButtonActionPerformed(null);
        } else {
        }
    }//GEN-LAST:event_emailTextFieldKeyPressed

    private void newPasswordFieldKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_newPasswordFieldKeyPressed
        if (KeyEvent.VK_ENTER == evt.getKeyCode()) {
            savePasswordButtonActionPerformed(null);
        } else {
        }
    }//GEN-LAST:event_newPasswordFieldKeyPressed

    private void reNewPasswordFieldKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_reNewPasswordFieldKeyPressed
        if (KeyEvent.VK_ENTER == evt.getKeyCode()) {
            savePasswordButtonActionPerformed(null);
        } else {
        }
    }//GEN-LAST:event_reNewPasswordFieldKeyPressed

    private void oldPasswordFieldKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_oldPasswordFieldKeyPressed
        if (KeyEvent.VK_ENTER == evt.getKeyCode()) {
            savePasswordButtonActionPerformed(null);
        } else {
        }
    }//GEN-LAST:event_oldPasswordFieldKeyPressed

    private void formKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_formKeyPressed
        if (KeyEvent.VK_ESCAPE == evt.getKeyCode()) {
            this.dispose();
        }
    }//GEN-LAST:event_formKeyPressed

    private void oldPasswordFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_oldPasswordFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_oldPasswordFieldActionPerformed

private void reNewPasswordFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_reNewPasswordFieldActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_reNewPasswordFieldActionPerformed
    /**
     * this is the valiation in the input to the edited user information
     */
    private void validateInputs() {
        String miss = "Missing the following\n";
        if (oldPasswordField.getText().equals("") || newPasswordField.getText().equals("") || reNewPasswordField.getText().equals("")) {
            if (oldPasswordField.getText().equals("")) {
                miss += "Old Password.\n";
            }
            if (newPasswordField.getText().equals("")) {
                miss += "New Password.\n";
            }
            if (reNewPasswordField.getText().equals("")) {
                miss += "Re-New Password\n";
            }
            JOptionPane.showMessageDialog(null, miss, "", JOptionPane.ERROR_MESSAGE);
        } else {
            if (newPasswordField.getText().equals(reNewPasswordField.getText())) {
                editPersonalStudentInfo();
            } else {
                newPasswordField.setForeground(Color.red);
                reNewPasswordField.setForeground(Color.red);
                JOptionPane.showMessageDialog(this, "New Password Miss match", "", JOptionPane.ERROR_MESSAGE);
            }
        }
    }

    /**
     * this is the method that modify the personal information
     */
    private void editPersonalStudentInfo() {
        try {
            Student editStudent = new Student();
            editStudent.setPassword(newPasswordField.getText());
            // use the three prameters upadte method when you want to edit the personal information
            // from the personal edit menu
            int update = new DBServices().update(editStudent,oldStudentEmail,oldPasswordField.getText());
            if (update == 1) {
                JOptionPane.showMessageDialog(rootPane, "Password changed successfuly.", "", JOptionPane.INFORMATION_MESSAGE);
                this.dispose();
            } else {
                JOptionPane.showMessageDialog(rootPane, "Password did't change.", "", JOptionPane.ERROR_MESSAGE);
            }
        } catch (Exception ex) {
            Logger.getLogger(changeMyPassword.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private javax.swing.JTextField emailTextField;
    private javax.swing.JPasswordField newPasswordField;
    private javax.swing.JLabel oldPassWordLabel;
    private javax.swing.JPasswordField oldPasswordField;
    private javax.swing.JLabel passWordOneLabel;
    private javax.swing.JLabel passWordTwoLabel;
    private javax.swing.JPasswordField reNewPasswordField;
    private javax.swing.JButton savePasswordButton;
    private javax.swing.JPanel titeldPanel;
    private javax.swing.JPanel titledPanel;
    private javax.swing.JLabel userNameLabel;
    // End of variables declaration//GEN-END:variables
}
